package com.sdry.web.controller.lz;
import com.sdry.model.llm.Department;
import com.sdry.model.lz.DeviceModel;
import com.sdry.model.lz.LzQueryCriteria;
import com.sdry.service.lz.DeviceService;
import com.sdry.utils.ResponseUtil;
import com.sdry.utils.StringUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import java.util.List;

/**
 * @ClassName DeviceController
 * @Description 设备信息
 * @Author lz
 * @Date 2018年9月18日 11:40:07
 * @Version 1.0
 */
@Controller
@RequestMapping("/device")
public class DeviceController {
    public static final Integer AFFECT_ROW = 0;
    @Resource
    DeviceService deviceService;

    /**
     * 新增设备信息
     * @param response
     * @param deviceModel 设备实体
     */
    @RequestMapping("/addDeviceInfo")
    public void addDeviceInfo(HttpServletResponse response, DeviceModel deviceModel) throws Exception {
        //String deviceNo = request.getParameter("deviceNo");
        //String deviceName = request.getParameter("deviceName");
    	if (deviceModel != null) {
			if(StringUtil.isEmpty(deviceModel.getBuyTime())){
				deviceModel.setBuyTime(null);
			}
			if(StringUtil.isEmpty(deviceModel.getOutTime())){
				deviceModel.setOutTime(null);
			}
		}
        Long affectRow = deviceService.addDeviceInfo(deviceModel);
        if (affectRow > AFFECT_ROW) {
            ResponseUtil.write(response, affectRow);
        } else {
            ResponseUtil.write(response, AFFECT_ROW);
        }
    }

    /**
     * 单个删除和批量删除
     * @param response
     * @param idArr id数组
     * @throws Exception
     */
    @RequestMapping("/deleteDeviceById")
    public void deleteDeviceById(HttpServletResponse response, @RequestParam("idArr[]") String[] idArr) throws Exception {
        for (int i = 0; i < idArr.length; i++) {
            Long affectRow = deviceService.deleteDeviceById(Long.parseLong(idArr[i]));
            if (affectRow > AFFECT_ROW) {
                ResponseUtil.write(response, affectRow);
            } else {
                ResponseUtil.write(response, AFFECT_ROW);
            }
        }
    }

    /**
     * 根据ID修改设备信息
     * @param response
     * @param deviceModel 选中行的设备
     * @throws Exception
     */
    @RequestMapping("/editDeviceInfoById")
    public void editDeviceInfoById(HttpServletResponse response, DeviceModel deviceModel) throws Exception {
    	if (deviceModel != null) {
			if(StringUtil.isEmpty(deviceModel.getBuyTime())){
				deviceModel.setBuyTime(null);
			}
			if(StringUtil.isEmpty(deviceModel.getOutTime())){
				deviceModel.setOutTime(null);
			}
		}
        Long affectRow = deviceService.editDeviceInfoById(deviceModel);
        if (affectRow > AFFECT_ROW) {
            ResponseUtil.write(response, affectRow);
        } else {
            ResponseUtil.write(response, AFFECT_ROW);
        }
    }

    /**
     * 分页查询设备信息
     * @param response
     * @param criteria 分页查询条件
     */
    @RequestMapping("/queryDeviceInfo")
    public void queryDeviceInfo(HttpServletResponse response, LzQueryCriteria criteria){
        String keyword01 = criteria.getKeyword01();
        String keyword02 = criteria.getKeyword02();
        if(keyword01 == null){
            keyword01 = "";
        }
        if(keyword02 == null){
            keyword02 = "";
        }
        criteria.setKeyword01(keyword01);
        criteria.setKeyword02(keyword02);
        List<DeviceModel> deviceList = deviceService.queryDeviceInfo(criteria);
        int count = deviceService.countDeviceInfo(criteria);
        JSONObject jsonObject = new JSONObject();
        JSONArray jsonArray = JSONArray.fromObject(deviceList);
        jsonObject.put("code", AFFECT_ROW);
        jsonObject.put("data", jsonArray);
        jsonObject.put("count", count);
        try {
            ResponseUtil.write(response, jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    /**
	 * 下拉框查找所有设备
     * @throws Exception 
	 */
	@RequestMapping("/queryAllDeviceList")
	public void queryAllDeviceList(HttpServletResponse response) throws Exception{
		List<DeviceModel> deviceModelList = deviceService.queryAllDeviceList();
		JSONArray jsonArray = JSONArray.fromObject(deviceModelList);
		if (deviceModelList.size() > 0) {
            ResponseUtil.write(response, jsonArray);
        } else {
            ResponseUtil.write(response, null);
        }
	}
	
	/**
	 * 下拉框查找所有部门
     * @throws Exception 
	 */
	@RequestMapping("/queryAllDeptList")
	public void queryAllDeptList(HttpServletResponse response) throws Exception{
		List<Department> departmentList = deviceService.queryAllDeptList();
		JSONArray jsonArray = JSONArray.fromObject(departmentList);
		if (departmentList.size() > 0) {
            ResponseUtil.write(response, jsonArray);
        } else {
            ResponseUtil.write(response, null);
        }
	}
	
	/**
	 * 根据ID查找设备信息
     * @throws Exception 
	 */
	@RequestMapping("/queryDeviceInfoByNo")
	public void queryDeviceInfoByNo(HttpServletResponse response, String deviceNo) throws Exception{
		List<DeviceModel> deviceModelList = deviceService.queryDeviceInfoByNo(deviceNo);
		if (deviceModelList.size() > 0) {
			JSONObject jsonObject = JSONObject.fromObject(deviceModelList.get(0));
			ResponseUtil.write(response, jsonObject);
		} else {
			ResponseUtil.write(response, "");
		}
	}
}
